<!--
 * @Author: 李天惊 uf_lee@163.com
 * @Date: 2022-08-23 15:25:52
 * @LastEditors: 李天惊 uf_lee@163.com
 * @LastEditTime: 2025-03-24 11:47:09
 * @FilePath: \global-brain\global-brain-admin\src\views\Login\Login-sso.vue
 * Copyright (c) 2022 by 李天惊 uf_lee@163.com, All Rights Reserved. 
-->
<script setup lang="ts">
import { ref, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useCache } from '@/hooks/web/useCache'
import { changeAuthority, changeRoute } from '@/utils/lee'
import { ucMyInfoApi, ucMyMenuAuthorityApi } from '@/api/my'
import { sysDictApi } from '@/api/dict'
import { ucMenuListApi } from '@/api/uc/menu'
import { usePermissionStore } from '@/store/modules/permission'
import type { RouteLocationNormalizedLoaded, RouteRecordRaw } from 'vue-router'
import { ucDeptListApi } from '@/api/uc/dept'
import { ucOrgDeptUserListApi } from '@/api/fa/user'

import { useAppStore } from '@/store/modules/app'
const appStore = useAppStore()

const permissionStore = usePermissionStore()
const { currentRoute, addRoute, push } = useRouter()
const { wsCache } = useCache()
const route = useRoute()
const token = route.query.token
const my_org_id = route.query.my_org_id
const redirect = ref<string>('')

watch(
  () => currentRoute.value,
  (route: RouteLocationNormalizedLoaded) => {
    redirect.value = route?.query?.redirect as string
  },
  {
    immediate: true
  }
)

const getDict = async () => {
  const res = await sysDictApi({ app_id: 2 }).catch(() => {})
  if (res) {
    // 字典转化
    let newDict = grouping(res.data)
    wsCache.set('dict', newDict)
  }
}
const grouping = (data: any) => {
  let map: any = {} // 处理过后的数据对象
  let temps: any = [] // 临时变量
  for (let key in data) {
    let ekey = data[key].dict_type // 根据dict_type分组
    temps = map[ekey] || [] // 如果map里面有这个key了，就取，没有就是空数组
    temps.push(data[key])
    map[ekey] = temps
  }
  return map
}
const getMyInfo = async () => {
  const res = await ucMyInfoApi()
    .catch(() => {})
    .finally(() => {})
  if (res) {
    wsCache.set('userInfo', res.data)
    await DeptList(res.data.default_org_id)
    await UserList(res.data.default_org_id, res.data.default_dept_id)
  }
}

const DeptList = async (value) => {
  const res = await ucDeptListApi({ org_id: value })
    .catch(() => {})
    .finally(() => {})
  if (res) {
    wsCache.set('myDeptList', res.data)
  }
}
const UserList = async (a, b) => {
  const res = await ucOrgDeptUserListApi({ org_id: a, dept_id: b })
    .catch(() => {})
    .finally(() => {})
  if (res) {
    wsCache.set('myUserList', res.data)
  }
  // await stateChangeUser(value)
}

const menuData = ref<any>([])
const getMenuList = async () => {
  const res = await ucMenuListApi({ app_id: 2 })
    .catch(() => {})
    .finally(() => {})
  if (res) {
    menuData.value = res.data
  }
}
const myMenuAuthority = ref<any>({})
const getMyMenuAuthority = async () => {
  const res = await ucMyMenuAuthorityApi({ app_id: 2 })
    .catch(() => {})
    .finally(() => {})
  if (res) {
    let newMenuAuthority = changeAuthority(res.data, 'menu_code')
    myMenuAuthority.value = newMenuAuthority
    wsCache.set('myMenuAuthority', newMenuAuthority)
    // wsCache.set(appStore.getUserInfo, res.data)
  }
}

// 转化路由并跳转
const getRoute = async () => {
  const routers: any = changeRoute(menuData.value, myMenuAuthority.value) //这里转化路由的时候判断了权限
  if (routers) {
    const { wsCache } = useCache()
    wsCache.set('myRouters', routers)
    await permissionStore.generateRoutes('admin', routers).catch(() => {})
    permissionStore.getAddRouters.forEach((route) => {
      addRoute(route as RouteRecordRaw) // 动态添加可访问路由表
    })
    permissionStore.setIsAddRouters(true)
    if (redirect.value == null || redirect.value == 'undefined' || redirect.value == '/404') {
      push({ path: permissionStore.addRouters[0].path })
    } else {
      push({ path: redirect.value })
    }
  }
}
// 初始化
const init = async () => {
  wsCache.set('token', token)
  wsCache.set('my_org_id', my_org_id)
  await getDict() // 获取字典相关数据
  await getMyInfo() // 获取当前用户信息
  await getMyMenuAuthority() //获取当前用户菜单权限
  await getMenuList() // 获取菜单的列表信息
  await getRoute() // 转化路由并跳转
  // console.log(getRoute)
}
init()
</script>

<template>
  <div>
    <div class="app-loading">
      <div class="app-loading-wrap">
        <div class="app-loading-title">
          <img src="/logo.png" class="app-loading-logo" alt="Logo" />
          <div class="app-loading-title">{{ appStore.getTitle }}-单点登录</div>
        </div>
        <div class="app-loading-item">
          <div class="app-loading-outter"></div>
          <div class="app-loading-inner"></div>
        </div>
      </div>
    </div>
  </div>
</template>

<style>
.app-loading {
  display: flex;
  width: 100%;
  height: 100%;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  background: #f0f2f5;
}

.app-loading .app-loading-wrap {
  position: absolute;
  top: 50%;
  left: 50%;
  display: flex;
  -webkit-transform: translate3d(-50%, -50%, 0);
  transform: translate3d(-50%, -50%, 0);
  justify-content: center;
  align-items: center;
  flex-direction: column;
}

.app-loading .app-loading-title {
  margin-bottom: 30px;
  font-size: 20px;
  font-weight: bold;
  text-align: center;
}

.app-loading .app-loading-logo {
  width: 100px;
  margin: 0 auto 15px auto;
}

.app-loading .app-loading-item {
  position: relative;
  display: inline-block;
  width: 60px;
  height: 60px;
  vertical-align: middle;
  border-radius: 50%;
}

.app-loading .app-loading-outter {
  position: absolute;
  width: 100%;
  height: 100%;
  border: 4px solid #2d8cf0;
  border-bottom: 0;
  border-left-color: transparent;
  border-radius: 50%;
  animation: loader-outter 1s cubic-bezier(0.42, 0.61, 0.58, 0.41) infinite;
}

.app-loading .app-loading-inner {
  position: absolute;
  top: calc(50% - 20px);
  left: calc(50% - 20px);
  width: 40px;
  height: 40px;
  border: 4px solid #87bdff;
  border-right: 0;
  border-top-color: transparent;
  border-radius: 50%;
  animation: loader-inner 1s cubic-bezier(0.42, 0.61, 0.58, 0.41) infinite;
}

@-webkit-keyframes loader-outter {
  0% {
    -webkit-transform: rotate(0deg);
    transform: rotate(0deg);
  }

  100% {
    -webkit-transform: rotate(360deg);
    transform: rotate(360deg);
  }
}

@keyframes loader-outter {
  0% {
    -webkit-transform: rotate(0deg);
    transform: rotate(0deg);
  }

  100% {
    -webkit-transform: rotate(360deg);
    transform: rotate(360deg);
  }
}

@-webkit-keyframes loader-inner {
  0% {
    -webkit-transform: rotate(0deg);
    transform: rotate(0deg);
  }

  100% {
    -webkit-transform: rotate(-360deg);
    transform: rotate(-360deg);
  }
}

@keyframes loader-inner {
  0% {
    -webkit-transform: rotate(0deg);
    transform: rotate(0deg);
  }

  100% {
    -webkit-transform: rotate(-360deg);
    transform: rotate(-360deg);
  }
}
</style>
